# 1. 添加cmake版本说明
cmake_minimum_required(VERSION 3.1.3)
# 2. 声明工程名称
project(user_server)

set(target user_server)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

# 3. 监测生成proto映射文件代码
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(proto_files user.proto base.proto file.proto)
foreach(proto_file ${proto_files})
    string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
    string(REPLACE ".proto" ".pb.h" proto_h ${proto_file})

    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/${proto_cc})
        message(${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/${proto_cc})
        message(${proto_h})
        add_custom_command(
            PRE_BUILD
            COMMAND protoc
            ARGS --cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/ -I${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_file}
            DEPENDS ${proto_path}/${proto_file}
            OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/${proto_cc}
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/${proto_cc}
        )
    endif()
    # 记录生成所需的文件
    list(APPEND proto_srcs ${CMAKE_CURRENT_SOURCE_DIR}/../proto_create/${proto_cc})
endforeach()

# 3. 监测生成odb映射文件代码
set(odb_path ${CMAKE_CURRENT_SOURCE_DIR}/../odb)
set(odb_files user.hxx)
foreach(odb_file ${odb_files})
    string(REPLACE ".hxx" "-odb.cxx" odb_cxx ${odb_file})
    string(REPLACE ".hxx" "-odb.hxx" odb_hxx ${odb_file})

    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/${odb_cxx})
        message(${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/${odb_cxx})
        message(${odb_hxx})
        add_custom_command(
            PRE_BUILD
            COMMAND odb
            ARGS -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ${odb_path}/${odb_file}
            DEPENDS ${odb_path}/${odb_file}
            OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/${odb_cxx}
            COMMAND mv ${odb_cxx} ${odb_hxx} *.sql *.ixx ${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/
            COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/${odb_cxx}
        )
    endif()
    # 记录生成所需的文件
    list(APPEND odb_srcs ${CMAKE_CURRENT_SOURCE_DIR}/../odb_create/${odb_cxx})
endforeach()

# 4. 获取源码目录下所有文件
set(src_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)

# 5. 声明目标以及依赖 --- 要将proto_srcs中的文件声明出来, 外部命令才会执行
add_executable(${target} ${src_files} ${proto_srcs} ${odb_srcs})

# 6. 设置需要连接的库
target_link_libraries(${target} -lgflags -lspdlog -lfmt 
                                -lbrpc -lssl -lcrypto -lprotobuf 
                                -lleveldb -letcd-cpp-api -lcpprest 
                                -lcurl -lodb-mysql -lodb -lodb-boost 
                                /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19 
                                -ljsoncpp
                                -lalibabacloud-sdk-core -lcpr -lelasticlient -lhiredis -lredis++)

# 7. 设置头文件默认搜索路径
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb_create)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../proto_create)


#8. 设置安装路径
INSTALL(TARGETS ${target} RUNTIME DESTINATION bin)